// pages/teacher/course-manage/course-manage.js
import { courses as mockCourses } from '../../../utils/mockData.js'

Page({
  data: {
    isEdit: false,
    courseId: null,
    form: {
      title: '',
      category: '',
      price: '',
      originalPrice: '',
      description: '',
      cover: '',
      tags: []
    },
    categories: ['编程开发', '设计创意', '商业管理', '语言学习', '考试认证', '职业技能'],
    selectedTags: [],
    allTags: ['实战', '项目', '基础', '进阶', '高级', '热门']
  },
  onLoad(options) {
    if (options.id) {
      // 编辑课程
      const courseId = parseInt(options.id)
      const course = mockCourses.find(c => c.id === courseId)
      if (course) {
        this.setData({
          isEdit: true,
          courseId: courseId,
          form: {
            title: course.title,
            category: course.category,
            price: course.price,
            originalPrice: course.originalPrice,
            description: course.description,
            cover: course.cover,
            tags: course.tags
          },
          selectedTags: course.tags
        })
      }
    }
  },
  handleTitleInput(e) {
    this.setData({ 'form.title': e.detail.value })
  },
  handlePriceInput(e) {
    this.setData({ 'form.price': e.detail.value })
  },
  handleOriginalPriceInput(e) {
    this.setData({ 'form.originalPrice': e.detail.value })
  },
  handleDescriptionInput(e) {
    this.setData({ 'form.description': e.detail.value })
  },
  selectCategory(e) {
    const category = e.currentTarget.dataset.category
    this.setData({ 'form.category': category })
  },
  toggleTag(e) {
    const tag = e.currentTarget.dataset.tag
    let tags = [...this.data.selectedTags]
    const index = tags.indexOf(tag)
    if (index > -1) {
      tags.splice(index, 1)
    } else {
      tags.push(tag)
    }
    this.setData({ 
      selectedTags: tags,
      'form.tags': tags
    })
  },
  chooseCover() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({ 'form.cover': res.tempFilePaths[0] })
        wx.showToast({ title: '封面已选择', icon: 'success' })
      }
    })
  },
  saveCourse() {
    const { form } = this.data
    
    // 表单验证
    if (!form.title) {
      wx.showToast({ title: '请输入课程名称', icon: 'none' })
      return
    }
    if (!form.category) {
      wx.showToast({ title: '请选择课程分类', icon: 'none' })
      return
    }
    if (!form.price) {
      wx.showToast({ title: '请输入课程价格', icon: 'none' })
      return
    }
    if (!form.description) {
      wx.showToast({ title: '请输入课程简介', icon: 'none' })
      return
    }
    
    wx.showLoading({ title: this.data.isEdit ? '保存中...' : '创建中...' })
    
    // 模拟保存
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: this.data.isEdit ? '保存成功' : '创建成功',
        icon: 'success'
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }, 1000)
  },
  deleteCourse() {
    wx.showModal({
      title: '确认删除',
      content: '删除后将无法恢复，确定要删除该课程吗？',
      confirmText: '删除',
      confirmColor: '#ff5722',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '删除中...' })
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({ title: '删除成功', icon: 'success' })
            setTimeout(() => {
              wx.navigateBack()
            }, 1500)
          }, 1000)
        }
      }
    })
  }
})
